<?php
// +----------------------------------------------------------------------
// |ZBPHP[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2019 http://www.benbenwangluo.com
// +----------------------------------------------------------------------
// | Author 似水星辰 [ 2630481389@qq.com ]
// +----------------------------------------------------------------------
// | 中犇软件 技术六部 出品
// +----------------------------------------------------------------------

namespace app\user\validate;

use think\Validate;

/**
 * 会员地址验证器
 * @package app\user\validate
 * @author 似水星辰 [2630481389@qq.com]
 */
class Address extends Validate
{
    //定义规则
    protected $rule = [
       "user_id|会员ID"=>[ 'require',"integer","gt"=>0],
       'name|收件人' =>[ 'require',"max"=>50,"checkName"],
       'mobile|手机号' =>[ 'require',"mobile"],
       'address|收件地址' =>[ 'require',"max"=>200,"checkContent"],
       "province|省份"=>[ 'require',"min"=>2,"max"=>20,"checkTitle"],
          "province_id|省份"=>[ 'require',"integer","gt"=>0],
       "city|城市"=>[ 'require',"min"=>2,"max"=>20,"checkTitle"],
          "city_id|城市"=>[ 'require',"integer","gt"=>0],
       "district|区县"=>[ 'require',"min"=>2,"max"=>20,"checkTitle"],
           "district_id|区县"=>[ 'require',"integer","gt"=>0],
        "is_default|是否默认"=>["in"=>[0,1]],
        "status|状态"=>["in"=>[0,1]],
        "postal_code|邮政编码"=>["integer","egt"=>0,"lt"=>999999],
        "national_code|国家编码"=>["integer","egt"=>0,"lt"=>1000],
        "label_name|标签"=>["max"=>5],
        "prefix|区号"=>["integer","egt"=>0,"elt"=>999999],
    ];

    protected $message = [
      'province_id' => '省份识别错误',
      'city_id' => '城市识别错误',
      'district_id' => '区县识别错误',
    ];

    // 定义验证场景
    protected $scene = [
        //'title' => ['title']
    ];
    
    protected function checkName($value,$rule,$data=[],$filed = ""){
       $check = preg_match('/^[\x{4e00}-\x{9fa5}\x{9fa6}-\x{9fef}\x{3400}-\x{4db5}\x{20000}-\x{2ebe0}a-zA-Z0-9\·\s]+$/u',$value);
        if(!$check){
            return ":attribute只可以填写中英文、数字、空格、圆点";
        }
        return true;
    }
    
    protected function checkTitle($value,$rule,$data=[],$filed = ""){
       $check = preg_match('/^[\x{4e00}-\x{9fa5}\x{9fa6}-\x{9fef}\x{3400}-\x{4db5}\x{20000}-\x{2ebe0}a-zA-Z0-9\_\—\-\s]+$/u',$value);
        if(!$check){
            return ":attribute只可以填写中英文、数字、空格";
        }
        return true;
    }
    protected function checkContent($value,$rule,$data=[],$filed = ""){
        $check = preg_match('/^[\x{4e00}-\x{9fa5}\x{9fa6}-\x{9fef}\x{3400}-\x{4db5}\x{20000}-\x{2ebe0}a-zA-Z0-9\_\—\-\s\,\，]+$/u',$value);
        if(!$check){
            return ":attribute只可以填写中英文、数字、空格和逗号";
        }
        return true;
    }
}
